package com.auoem.radio.base;

import android.app.Application;
import android.content.Intent;
import android.serialport.DeviceControlSpd;

import com.auctus.am.developer.AuHelper;
import com.auctus.am.framework.entity.AmAttr;
import com.auctus.am.framework.imp.AmCrashHandler;
import com.auctus.am.framework.interfaces.ComCallback;
import com.auctus.am.framework.utils.LogM;
import com.auoem.radio.common.utils.ScreenUtils;
import com.auoem.radio.service.PttService;

import java.io.IOException;

/**
 * 20240417  去除gpio10的控制
 * 20240418  去除关闭服务
 */
public class OemApplication extends Application {

    private static final String TAG = "OemApplication";
    private static OemApplication mApplication;

    private DeviceControlSpd DevCtrlHorn1;
    private DeviceControlSpd DevCtrlHorn2;
    private DeviceControlSpd DevCtrlHorn3;
    private DeviceControlSpd DevCtrlPort1;


    private Intent startService;

    @Override
    public void onCreate() {
        super.onCreate();

        try {
            DevCtrlPort1 = new DeviceControlSpd(DeviceControlSpd.PowerType.EXPAND, 8);

            DevCtrlHorn1 = new DeviceControlSpd(DeviceControlSpd.PowerType.EXPAND, 5);
            DevCtrlHorn2 = new DeviceControlSpd(DeviceControlSpd.PowerType.EXPAND, 6);
            DevCtrlHorn3 = new DeviceControlSpd(DeviceControlSpd.PowerType.EXPAND, 7);

            DevCtrlPort1.PowerOnDevice();

            DevCtrlHorn2.PowerOnDevice();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        AmCrashHandler.getInstance().init();
        mApplication = this;
        /*Step 1: A6初始化上下文*/
        AuHelper.getInstance().onAppCreate(this);
        ScreenUtils.init(this);
        LogM.init(this);
        LogM.setFilterType(LogM.FILTER_TYPE_LEVEL);


    }

    @Override
    public void onTerminate() {
        doOnDestroy();
        LogM.d(TAG, "onTerminate");
        super.onTerminate();
    }

    public static OemApplication getApplication() {
        return mApplication;
    }

    /* 在进入主页前初始化A6模块，通常在闪屏页进行 */
    public void initAm() {
        /*Step-2.1 绑定A6与定制系统的属性*/
        AmAttr amAttr = new AmAttr(AuConstants.AM_PATH_DEV, AuConstants.AM_PATH_POWER_EN, AuConstants.AM_PATH_POWER_CS, AuConstants.AM_PATH_PTT, AuConstants.ACTION_PTT_DOWN, AuConstants.ACTION_PTT_UP);
//        amAttr.pathPowerMic = AuConstants.AM_PATH_POWER_MIC;
//        amAttr.pathPowerTx = AuConstants.AM_PATH_POWER_TX;
        amAttr.pathPowerMic = AuConstants.AM_PATH_POWER_EN;
        amAttr.pathPowerTx = AuConstants.AM_PATH_POWER_CS;
        /*Step-2.2初始化A6模块*/
        AuHelper.getInstance().initOnFlashActivity(this, amAttr, (msg, code, inf) -> {
            if (code == ComCallback.CODE_SUCCESS) {

            } else {
                //todo 不应该失败

            }
        });
    }

    /*退出程序前结束A6模块*/
    public void doOnDestroy() {
        LogM.d(TAG, "doOnDestroy");

        //停止这个服务
        startService = new Intent(getApplication(), PttService.class);
        //stopService(startService);

        try {
            DevCtrlHorn2.PowerOffDevice();

            DevCtrlPort1.PowerOffDevice();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        DevCtrlHorn2 = null;

        DevCtrlPort1 = null;

        AuHelper.getInstance().onAppDestroy();
        System.exit(0);
    }

    public DeviceControlSpd getDevCtrlHorn1() {
        return DevCtrlHorn1;
    }

    public DeviceControlSpd getDevCtrlHorn2() {
        return DevCtrlHorn2;
    }

    public DeviceControlSpd getDevCtrlHorn3() {
        return DevCtrlHorn3;
    }
}
